package cn.edu.seu.cose.qol.model.dao.database;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class QolSchemaConnectionFactory {
	
	private static QolSchemaConnectionFactory _instance;
	
	public static QolSchemaConnectionFactory getInstance() {
		if (_instance == null) {
			_instance = new QolSchemaConnectionFactory();
		}
		
		return _instance;
	}
	
	protected QolSchemaConnectionFactory() {}

	public Connection newConnection() throws SQLException {
		try {
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			DataSource ds = (DataSource) envCtx.lookup("jdbc/qol_schema");
			Connection conn = ds.getConnection();
			return conn;
		} catch (NamingException e) {
			throw new RuntimeException(e.getExplanation());
		}
	}

}
